<?php
class InitAclController extends AppController
{
	var $name = 'InitAcl';
	var $component = array('Acl');
	var $uses = array('User');

	function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allowedActions = array('*');
	}

	/**
	 * Rebuild the Acl based on the current controllers in the application
	 *
	 * @return void
	 */
	function buildAcl() {
		$log = array();

		$aco =& $this->Acl->Aco;
		$root = $aco->node('controllers');
		if (!$root) {
			$aco->create(array('parent_id' => null, 'model' => null, 'alias' => 'controllers'));
			$root = $aco->save();
			$root['Aco']['id'] = $aco->id;
			$log[] = 'Created Aco node for controllers';
		} else {
			$root = $root[0];
		}

		App::import('Core', 'File');
		$Controllers = Configure::listObjects('controller');
		$appIndex = array_search('App', $Controllers);
		if ($appIndex !== false ) {
			unset($Controllers[$appIndex]);
		}
		$baseMethods = get_class_methods('Controller');
		$baseMethods[] = 'buildAcl';

		// look at each controller in app/controllers
		foreach ($Controllers as $ctrlName) {
			 
			App::import('Controller', $ctrlName);
			$ctrlclass = $ctrlName . 'Controller';
			$methods = get_class_methods($ctrlclass);
			// find / make controller node
			$controllerNode = $aco->node('controllers/' . $ctrlName);
			if (!$controllerNode) {
				$aco->create(array('parent_id' => $root['Aco']['id'], 'model' => null, 'alias' => $ctrlName));
				$controllerNode = $aco->save();
				$controllerNode['Aco']['id'] = $aco->id;
				$log[] = 'Created Aco node for ' . $ctrlName;
			} else {
				$controllerNode = $controllerNode[0];
			}

			// clean the methods. to remove those in Controller and private actions.
			foreach ($methods as $k => $method) {
				if (strpos($method, '_', 0) === 0) {
					unset($methods[$k]);
					continue;
				}
				if (in_array($method, $baseMethods)) {
					unset($methods[$k]);
					continue;
				}
				$methodNode = $aco->node('controllers/' . $ctrlName . '/' . $method);
				if (!$methodNode) {
					$aco->create(array('parent_id' => $controllerNode['Aco']['id'], 'model' => null, 'alias' => $method));
					$methodNode = $aco->save();
					$log[] = 'Created Aco node for ' . $method;
				}
			}
		}
		debug($log);
	}

	function buildAcl2() {
		$log = array();

		$aco =& $this->Acl->Aco;
		$root = $aco->node('controllers');
		if (!$root) {
			$aco->create(array('parent_id' => null, 'model' => null, 'alias' => 'controllers'));
			$root = $aco->save();
			$root['Aco']['id'] = $aco->id;
			$log[] = 'Created Aco node for controllers';
		} else {
			$root = $root[0];
		}

		$Controllers = array('Images','News','Polls','Rankings','Galleries','Programas','Users');

		// look at each controller in app/controllers
		foreach ($Controllers as $ctrlName) {
			 
			$methods = array(
			    'Polls'=>array('index','add','edit','delete','view'),
				'Users'=>array('index','add','edit','delete','view'),
				'News'=>array('index','add','edit','delete','view'),
			    'Rankings'=>array('index','add','edit','delete','view'),
				'Programas'=>array('index','add','edit','delete','view'),
				'Galleries'=>array('index','add','edit','delete','view','agregar_imagen'),
            	'Images'=>array('add','delete','moveup','movedown')); 
			// find / make controller node
			$controllerNode = $aco->node('controllers/' . $ctrlName);
			if (!$controllerNode) {
				$aco->create(array('parent_id' => $root['Aco']['id'], 'model' => null, 'alias' => $ctrlName));
				$controllerNode = $aco->save();
				$controllerNode['Aco']['id'] = $aco->id;
				$log[] = 'Created Aco node for ' . $ctrlName;
			} else {
				$controllerNode = $controllerNode[0];
			}

			// clean the methods. to remove those in Controller and private actions.
			foreach ($methods[$ctrlName] as $k => $method) {

				$methodNode = $aco->node('controllers/' . $ctrlName . '/' . $method);
				if (!$methodNode) {
					$aco->create(array('parent_id' => $controllerNode['Aco']['id'], 'model' => null, 'alias' => $method));
					$methodNode = $aco->save();
					$log[] = 'Created Aco node for ' . $method;
				}
			}
		}
		debug($log);
	}
	/**
	 * Get the names of the plugin controllers
	 *
	 * This function will get an array of the plugin controller names, and
	 * also makes sure the controllers are available for us to get the
	 * method names by doing an App::import for each plugin controller.
	 *
	 * @return array List of plugin names
	 */

	function _getPluginControllerNames(){
		App::import('Core', 'File', 'Folder');
		$paths = Configure::getInstance();
		$folder =& new Folder();
		// change directory to the plugins
		$folder->cd(APP . 'plugins');
		// get a list of the files that have a file name that ends with controller.php
		$files = $folder->findRecursive('.*_controller\.php');
		// Get the list of plugins
		$Plugins = Configure::listObjects('plugin');
		debug($files);
		// loop through the controllers we found in the plugins directory
		foreach ($files as $f => $fileName) {
			// get the base file name
			$file = basename($fileName);
			// get the controller name
			$file = Inflector::camelize(substr($file, 0, strlen($file) - strlen('_controller.php')));

			// loop through the plugins
			foreach ($Plugins as $pluginName) {
				if (preg_match('/^' . $pluginName . '/', $file)){
					// first, get rid of the App controller for the plugin
					// we do this because the app controller is never called directly
					if (preg_match('/^' . $pluginName . 'App/', $file)) {
						unset($files[$f]);
					} else {
						if (!App::import('Controller', $pluginName . '.' . $file)) {
							debug('Error importing ' . $file . ' for plugin ' . $pluginName);
						}
						// now prepend the Plugin name
						// this is required to allow us to fetch the method names
						$files[$f] = $file;
					}
					break;
				}
			}
		}
		debug($files);

		$files = array ('Images','Carritos','Productos','Productos/Categorias');
		return $files;
	}


	function initDB() {
		$group = $this->User->Group;
		//Allow admins to everything
		$group->id = 1;
		$this->Acl->allow($group, 'controllers');
	}

}
?>